package com.mdd.front.controller;

import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.coupon.ICouponService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.vo.coupon.CouponListVo;
import com.mdd.front.vo.goods.GoodsDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/coupon")
@Api(tags = "优惠券管理")
public class CouponController {

    @Resource
    private ICouponService iCouponService;

    /**
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return Object
     */
    @GetMapping("/list")
    @ApiOperation(value = "优惠券管理 - 列表")
    public PageResult<CouponListVo> list(@Validated PageParam pageParam,
                           @RequestParam Map<String, String> params){
        PageResult<CouponListVo> list = iCouponService.list(pageParam, params);
        return list;
    }

    /**
     * @param pageParam 分页参数
     * @param type 类型， 0--未使用   1-已使用   2-已过期
     * @return
     */
    // 获取当前用户的所有优惠券
    @GetMapping("/user/coupon")
    @ApiOperation("获取当前用户的所有优惠券")
    public PageResult<CouponListVo> getUserCoupon(@Validated PageParam pageParam, @RequestParam("type") Integer type){
        return iCouponService.getUserCoupon(pageParam, LikeFrontThreadLocal.getUserId(), type);
    }

    @PostMapping("/receive/coupon/{id}")
    @ApiOperation("用户领取优惠券, 二维码调用这个接口")
    public void receiveCoupon(@PathVariable("id") Integer id){
        iCouponService.receiveCoupon(id, LikeFrontThreadLocal.getUserId());
    }

    // 使用兑换码兑换优惠券
    @PostMapping("/collect/code/{code}")
    @ApiOperation("用户领取优惠券")
    public void collectCoupon(@PathVariable("code") String code){
        iCouponService.collectCoupon(code, LikeFrontThreadLocal.getUserId());
    }

    @PostMapping("/goods/coupon/{goodsId}")
    @ApiOperation("获取当前用户的所有可用优惠券")
    public List<CouponListVo> goodsCoupon(@PathVariable(value = "goodsId") Integer goodsId) {
        return iCouponService.goodsCoupon(goodsId, LikeFrontThreadLocal.getUserId());
    }

    @PostMapping("/getGoodsByCouponId/{id}")
    @ApiOperation("点击优惠券去使用的接口")
    public List<GoodsDetailVo> getGoodsByCouponId(@PathVariable("id") Integer couponId) {
        return iCouponService.getGoodsByCouponId(couponId);
    }

    @GetMapping("/getCouponByCouponCode")
    @ApiOperation("首页获取优惠券详情")
    public AjaxResult getCouponByCouponCode(@RequestParam(value = "couponCode", required = false) String couponCode,
                                            @RequestParam(value = "couponId", required = false) Integer couponId) {
        return AjaxResult.success(iCouponService.getCouponByCouponCode(couponCode, couponId));
    }
}
